package org.bonorsoft.modules.statistical.util;

import org.bonorsoft.common.constant.StatisticalConstant;
import org.bonorsoft.modules.statistical.entity.StatisticalBasicData;

import java.util.List;
import java.util.function.Function;

public class CityUtil {
    //指定省份顺序
    private static final List<String> regulationOrder = StatisticalConstant.REGULATION_ORDER;

    public static void sortedByProvince(List<StatisticalBasicData> enrollmentGroups) {

        Function<StatisticalBasicData, String> getProvince = (oneGroup) -> {
            String province;
            String provinceTemp = oneGroup.getSchoolLocationThree();
            if (provinceTemp.contains(",")) {
                String[] provinces = provinceTemp.replaceAll("\\s*", "").split(",");
                province = provinces[0].substring(0, 2);

                for (String s : provinces) {
                    if (regulationOrder.indexOf(province) == -1) {
                        province = s.substring(0, 2);
                    } else if (regulationOrder.indexOf(province) > regulationOrder.indexOf(s.substring(0, 2))) {
                        province = s.substring(0, 2);
                    }
                }
            } else {
                province = provinceTemp.substring(0, 2);
            }

            return province;
        };

        enrollmentGroups.sort((o1, o2) -> {
            String o1_province = getProvince.apply(o1); //o1中最优先的省份
            String o2_province = getProvince.apply(o2); //o2中最优先的省份

            int o1_index = regulationOrder.indexOf(o1_province);
            int o2_index = regulationOrder.indexOf(o2_province);

            if (o1_index == -1) {
                return 1;
            }

            if (o2_index == -1) {
                return -1;
            }

            return o1_index - o2_index;
        });
    }
}
